using System;
using System.Configuration;
using System.Globalization;
using System.Web;
using System.Web.Mail;
using System.Web.UI;
using System.Xml;

using umlungu.engineX.emailX;

/*	-----------------------------------------------------------------------	
	Copyright:	umlungu consulting (pty) ltd
	Author:		Alan Benington
	Started:	2008-06-14
	Status:		release	
	Version:	2.0.0
	Build:		20080614
	License:	GNU General Public License
	-----------------------------------------------------------------------	*/

/*	-----------------------------------------------------------------------	
	Development Notes:
	==================
	20080614:	Initial release.
	---------------------------------------------------------------------------	*/

namespace umlungu.engineX.blogX.WebsiteX {
	/// <summary>
	/// Description of the WebsiteXSurvey class.
	/// <para>Additional information about the class</para>
	/// </summary>
	public class WebsiteXSurvey : x_broker {
		#region Invisible properties
		private const string surveySubj = "Customer survey";
		private const string surveyTo = "survey@WebsiteX.co.za";
		private const string error_contact_send = "Survey email sending error: ";
		private string filename;
		private XmlDocument results;
		private DateTime now;
		#endregion

		#region Visible properties
		#endregion

		#region Constructors/Destructors
		/// <overloads>Constructor</overloads>
		/// <summary>Default constructor</summary>
		/// <param name="thispage">The web application Page object</param>
		public WebsiteXSurvey(displayX thispage, string file) : base(thispage) {
			now = DateTime.Now;
			filename = UIPage.Server.MapPath(String.Concat("survey/", file, ".xml"));
			results = new XmlDocument();
			results.Load(filename);
		}
		#endregion

		#region Public methods
		public override void Process() {
			// test for spam by checking for some input values
			string feedback = _GetRequest(true, "Feedback", "");
			bool isSpam = SpamCheck(feedback, false);
			string deskinp = _GetRequest(true, "Desktop_Input", "");
			bool isSpamDesk = SpamCheck(deskinp, false);
			if (!(isSpam || isSpamDesk)) {
				send();
				record();
			}
		}
		public override void Process(bool add) {
			if (add)
				UIPage.Content.AppendChild(UIPage.Document.ImportNode(results.DocumentElement, true));
		}
		#endregion
		
		#region Protected methods
		#endregion
				
		#region Private methods
		/// <summary>
		/// Emails a content request to the configured email address
		/// </summary>
		private void record() {
			XmlNode reslts = results.SelectSingleNode("//results");
			XmlNode template = reslts.SelectSingleNode("result[@id='0']");
			XmlNode reslt = template.CloneNode(true);
			// Add field values
			String[] fldnames = UIPage.Request.Form.AllKeys;
			foreach (string fldname in fldnames)  {
				XmlNode valnode = reslt.SelectSingleNode(fldname);
				if (valnode == null)
					valnode = reslt.SelectSingleNode(String.Concat("@", fldname));
				if (valnode != null)
					valnode.InnerText = UIPage.Request.Form[fldname];
			}
			int id = Convert.ToInt32(reslts.LastChild.SelectSingleNode("@id").InnerText) + 1;
			reslt.SelectSingleNode("@id").InnerText = id.ToString();
			reslt.SelectSingleNode("@date").InnerText = now.ToString("s", DateTimeFormatInfo.InvariantInfo);
			reslts.AppendChild(reslt);
			results.Save(filename);
		}
		/// <summary>
		/// Records to an xml spreadsheet
		///	NB: This fails on the select for the Row
		/// </summary>
		private void recordxls() {
			XmlNode reslts = results.SelectSingleNode("//Worksheet/Table");
			XmlNode template = reslts.SelectSingleNode(String.Concat("Row[@identifier='template']"));
			XmlNode reslt = template.CloneNode(true);
			// Add field values
			String[] fldnames = UIPage.Request.Form.AllKeys;
			foreach (string fldname in fldnames)  {
				XmlNode valnode = reslt.SelectSingleNode(String.Concat("Cell/Data/@identifier='", fldname, "'"));
				if (valnode != null)
					valnode.InnerText = UIPage.Request.Form[fldname];
			}
			reslts.AppendChild(reslt);
			results.Save(filename);
		}
		/// <summary>
		/// Records to an xml spreadsheet
		///	NB: This fails on the select for the Row
		/// </summary>
		private void recordcsv() {
			
		}
		/// <summary>
		/// Emails a content request to the configured email address
		/// </summary>
		private void send() {
			try {
				x_email thisemail = new x_email();
				thisemail.Bcc = "alanben@umlungu.com";
				thisemail.Subject = surveySubj;
				thisemail.To = surveyTo;
				thisemail.Type = x_emailtype.html;
				thisemail.Send(UIPage.Request, _GetTemplate("emailX.xsl"));
			}
			catch(System.Exception e) {
				throw(new x_exception("error_contact_send", String.Concat(error_contact_send, e.Message)));
			}
		}
		#endregion
	}
}
